#include <stdio.h>

#define K 6
#define N 16

void siftup(int *data, int i)
{
	int p, tmp;

	while (1) {
		if (!i)
			break;
		p = (i - 1) >> 1;
		if (data[p] <= data[i])
			break;

		tmp = data[i];
		data[i] = data[p];
		data[p] = tmp;
		i = p;
	}
}

void siftdown(int *data, int i, int end)
{
	int left, tmp;

	while (1) {
		left = (i << 1) + 1;
		if (left > end)
			break;
		if (left + 1 <= end && data[left + 1] < data[left])
			left++;
		if (data[left] >= data[i])
			break;

		tmp = data[left];
		data[left] = data[i];
		data[i] = tmp;
		i = left;
	}
}

int main() {
	int data[N] = {5, 8, 9, 4, 3, 1, 2, 6,
		     15, 18, 19, 23, 23, 21, 12, 16};
	int i = 0, tmp;
	for (i = 1; i < K; i++)
		siftup(data, i);
	for (i = K; i < N; i++) 
		if (data[i] > data[0]) {
			tmp = data[0];
			data[0] = data[i];
			data[i] = tmp;
			siftdown(data, 0, K-1);
		}
	for (i = 0; i < K; i++)
		printf("%d\t", data[i]);
	
	return 0;
}

